<?php

namespace App\Http\Controllers;

use App\Models\ESStation;
use Illuminate\Http\Request;

class ESStationController extends Controller
{
    public function index(Request $request)
    {
        $data = ESStation::with('enterprise')->requestCriteria($request)->paginate($request->query('limit'));
        return $this->respondSuccess($data);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'enterprise_id' => 'required',
            'name' => 'required',
            'director' => 'required',
            'mobile' => 'required',
        ]);

        $energyStation = ESStation::create($request->input());
        return $this->respondSuccess($energyStation);
    }

    public function show(Request $request, ESStation $esStation)
    {
        if($request->limit){
            return $this->respondSuccess($esStation->ESUnit()->paginate($request->query('limit')));
        }
        $esStation['count'] = $esStation->ESUnit()->count();
        return $this->respondSuccess($esStation->load('enterprise'));
    }

    public function update(Request $request, ESStation $esStation)
    {        
        $data = $esStation->update($request->input());
        return $this->respondSuccess($data);
    }


    public function destroy(ESStation $esStation)
    {
        $data = $esStation->delete();
        return $this->respondSuccess($data);
    }
}
